// hyb_yl/krecovery/pages/postoperative_order/postoperative_order.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    keshi_two: "",
    typeid: "",
    zid: "",
    content: "",
    j_id: "",
    shuqianimg: "",
    shuhouimg: "",
    biaoqian: "",
    green_money: "",
    time_length: "",
    keyword: "",
    numcard: "",
    sex: "",
    checked:false,
    names: "",
    tel: "",
    region: "",
    ks_title: "",
    title: "",
    agentname: "",
    hid: "",
    green_title: "",
    green_thumb: "",
    age: "",
    obtain_index: 0,
    name:"",
    telphone:"",
    city:"",
    xxaddress:"",
    equipment:"",
    equipment_id:""
  },
  // 术后服务协议
  showshuhouxieyi:function(){
    this.setData({
      agreement_state:true,
      checked:!this.data.checked
    })
  },
  closezhe:function(){
    this.setData({
      agreement_state:false
    })
  },
  // 放大图片
  big_img:function(e){
      var imglist=e.currentTarget.dataset.imglist;
      var img=e.currentTarget.dataset.img;
      wx.previewImage({
        urls: imglist,
        current:img
      })
  },
  city_pick: function (e) {
    console.log(e.detail.value.join('-'))
    this.setData({
      city: e.detail.value.join('-')
    })
  },
  obtain_tab: function (e) {
    var index = e.currentTarget.dataset.index;
    this.setData({
      obtain_index: index,
    })
  },
  address_location: function (e) {
    wx.openLocation({
      latitude: e.currentTarget.dataset.latitude, // 纬度，范围为-90~90，负数表示南纬
      longitude: e.currentTarget.dataset.longitude, // 经度，范围为-180~180，负数表示西经
      scale: 28, // 缩放比例
    })
  },
  // 详细地址
  address_input:function(e){
    this.setData({
      xxaddress:e.detail.value
    })
  },
  // 收货人输入
  name_input:function(e){
    this.setData({
      name:e.detail.value
    })
  },
  // 联系方式输入
  tel_input:function(e){
    this.setData({
      telphone:e.detail.value
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var bgc=wx.getStorageSync('color');
    wx.setNavigationBarColor({
      frontColor: "#ffffff",
      backgroundColor: bgc,
    })
    this.setData({
      bgc:bgc
    })
    that.setData({
      keshi_two: options.keshi_two,
      typeid: options.typeid,
      zid: options.zid,
      content: options.content,
      j_id: options.j_id,
      shuqianimg: options.shuqianimg,
      shuhouimg: options.shuhouimg,
      biaoqian: options.biaoqian,
      green_money: options.green_money,
      time_length: options.time_length,
      keyword: options.keyword,
      numcard: options.numcard,
      sex: options.sex,
      names: options.names,
      tel: options.tel,
      region: options.region,
      ks_title: options.ks_title,
      title: options.title,
      agentname: options.agentname,
      hid: options.hid,
      green_title: options.green_title,
      green_thumb: options.green_thumb,
      age: options.age
    })
    var shuqianimg = JSON.parse(options.shuqianimg);
    var shuhouimg = JSON.parse(options.shuhouimg);
    that.setData({
      shuqianimg: shuqianimg,
      shuhouimg: shuhouimg,
    })
    that.getequipment();
    that.getHospital();
    that.getAddress();
    app.util.request({
      'url': 'entry/wxapp/index.detailserones',
      data: {
        ser_key: 'shukoukangfu'
      },
      success: function (res) {
        console.log(res)
        that.setData({
          detail: res.data
        })
      },
      fail: function (err) {
        console.log(err)
      },
    })
  },
  // 获取设备
  getequipment:function(){
    var that = this;
    app.util.request({
      'url':"entry/wxapp/green.getequipment",
      data:{
        id:that.data.typeid
      },
      success:function(res)
      {
        that.setData({
          equipment:res.data,
        })
        var green_money = that.data.green_money;
        var equipment_id = "";
        var total = parseFloat(green_money);
        for(var i=0;i<res.data.length;i++)
        {
          total += parseFloat(res.data[i]['money']);
          equipment_id += res.data[i].id+',';
        }
        that.setData({
          green_money:green_money,
          equipment_id:equipment_id,
          total:total
        })
      }
    })
  },
  // 获取机构详情
  getHospital:function(){
    var that = this;
    app.util.request({
      'url':"entry/wxapp/green.Hospital_detail",
      data:{
        hid:that.data.hid
      },
      success:function(res){
        that.setData({
          hospital:res.data
        })
      }
    })
  },
  // 获取用户地址
  getAddress:function(){
    var that = this;
    app.util.request({
      'url':"entry/wxapp/green.user_address",
      data:{
        openid:wx.getStorageSync('openid'),
      },
      success:function(res){
        console.log(res)
        if(res.data)
        {
          that.setData({
            address:res.data,
            city:res.data.city,
            name:res.data.userName,
            telphone:res.data.userPhone,
            xxaddress:res.data.xxaddress,
            a_id:res.data.addressId
          })
        }
        
      }
    })
  },
  topay: function () {
    var that = this;
    var keshi_two = that.data.keshi_two;
    var typeid = that.data.typeid;
    var zid = that.data.zid;
    var content = that.data.content;
    var j_id = that.data.j_id;
    var shuqianimg = JSON.stringify(that.data.shuqianimg);
    var shuhouimg = JSON.stringify(that.data.shuhouimg);
    var biaoqian = that.data.biaoqian;
    var green_money = that.data.green_money;
    var time_length = that.data.time_length;
    var keyword = that.data.keyword;
    var sex = that.data.sex;
    var tel = that.data.tel;
    var hid = that.data.hid;
    var names = that.data.names;
    var obtain_index = that.data.obtain_index;
    var name = that.data.name;
    var telphone = that.data.telphone;
    var city = that.data.city;
    var xxaddress = that.data.xxaddress;
    var total = that.data.total
    console.log(name);
    if(that.data.checked == false)
    {
      wx.showToast({
        title: '请选阅读协议',
      })
      that.setData({
        checked:true,
        agreement_state:true
      })
      return;
    }
    if(obtain_index == 0)
    {
      if(name == '')
      {
        wx.showToast({
          title: '请输入收件人',
        })
        return;
      }else if(telphone == '')
      {
        wx.showToast({
          title: '请输入联系电话',
        })
        return;
      }else if(/^1[34578]\d{9}$/.test(telphone) == false)
      {
        wx.showToast({
          title: '手机号不正确',
        })
        return;
      }else if(city == '')
      {
        wx.showToast({
          title: '请选择城市',
        })
        return;
      }else if(xxaddress == '')
      {
        wx.showToast({
          title: '请输入详细地址',
        })
        return;
      }
    }
    app.util.request({
      'url': "entry/wxapp/green.shuhoukangfu",
      data: {
        keshi_two: keshi_two,
        typeid: typeid,
        zid: zid,
        content: content,
        j_id: j_id,
        shuqianimg: shuqianimg,
        shuhouimg: shuhouimg,
        biaoqian: biaoqian,
        green_money: green_money,
        total:total,
        time_length: time_length,
        keyword: keyword,
        sex: sex,
        names: names,
        tel: tel,
        hid: hid,
        openid: wx.getStorageSync('openid'),
        age: that.data.age,
        name:name,
        method:obtain_index,
        telphone:telphone,
        city:city,
        xxaddress:xxaddress,
        equiment:that.data.equipment_id 

      },
      success: function (res) {
        var orders = res.data.orders
        var back_orser = res.data.back_orser
        var id = res.data.id
        if (total == 0 || total == '0.00') {
          app.util.request({
            'url': 'entry/wxapp/zhuanjia.getdocmbtxing',
            data: {
              id: id,
              val: content,
              near: '术后康复',
              zid: that.data.zid
            },
            success: function (res) {
              console.log(res)
              wx.navigateTo({
                url: '/hyb_yl/krecovery/pages/postoperative_ordercont/postoperative_ordercont?back_orser=' + back_orser+'&key_words=shuhoukangfu',
              })
            }
          })
        } else {
          app.util.request({
            'url': 'entry/wxapp/yuyue.paywenzhen',
            header: {
              "Content-Type": "application/xml"
            },
            method: "GET",
            data: {
              openid: wx.getStorageSync('openid'),
              orders:orders,
              z_tw_money: total,
              key_words: that.data.keyword
            },
            success: function (res) {
              console.log(res);
              wx.requestPayment({
                'timeStamp': res.data.timeStamp,
                'nonceStr': res.data.nonceStr, //字符串随机数
                'package': res.data.package,
                'signType': res.data.signType,
                'paySign': res.data.paySign,
                success: function (res) {
                  console.log(res)

                },
                fail: function (res) {
                  console.log(res)

                },
                'complete': function (res) {
                  //更新问诊时间
                  if (res.errMsg == 'requestPayment:ok') {

                    app.util.request({
                      'url': 'entry/wxapp/green.pay_orders',
                      data: {
                        orders: orders,
                      },
                      success: function (res) {
                        console.log(res)

                      }
                    })
                    //模板消息提醒
                    app.util.request({
                      'url': 'entry/wxapp/zhuanjia.getdocmbtxing',
                      data: {
                        id: id,
                        val: content,
                        near: '术后康复',
                        zid: that.data.zid
                      },
                      success: function (res) {
                        console.log(res)
                        wx.navigateTo({
                          url: '/hyb_yl/krecovery/pages/postoperative_ordercont/postoperative_ordercont?back_orser=' + back_orser+'&key_words=shuhoukangfu',
                        })
                      }
                    })

                    return;
                  }
                }
              })
            }
          })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})